<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }"><i class="el-icon-s-home"></i>后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加管理员</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
<!--model	表单数据对象  rules	表单验证规则-->
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <!--  rules对象的属性名称与prop的名称要对应匹配    -->
      <el-form-item label="用户名" prop="username">
        <el-input v-model="ruleForm.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="ruleForm.password"></el-input>
      </el-form-item>
      <el-form-item label="昵称" prop="nickname">
        <el-input v-model="ruleForm.nickname"></el-input>
      </el-form-item>
      <el-form-item label="头像路径" prop="avatar">
        <el-input v-model="ruleForm.avatar"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="ruleForm.phone"></el-input>
      </el-form-item>
      <el-form-item label="电子邮箱" prop="email">
        <el-input v-model="ruleForm.email"></el-input>
      </el-form-item>
      <el-form-item label="简介" prop="description">
        <el-input v-model="ruleForm.description"></el-input>
      </el-form-item>
      <el-form-item label="是否启用" prop="enable">
        <el-switch
            v-model="ruleForm.enable"
            :active-value="1"
            :inactive-value="0"
            active-color="#13ce66"
            inactive-color="grey">
        </el-switch>
      </el-form-item>
      <el-form-item label="角色id" prop="roleIds">
          <el-select v-model="ruleForm.roleIds" multiple placeholder="请选择">
            <el-option
                v-for="item in options"
                :key="item.id"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "AddAdminView",
  data() {
    return {
      ruleForm: {
        username: '张三',
        password: '123',
        nickname: '',
        avatar: '',
        phone: '123',
        email: '123@qq.com',
        description: '',
        enable: '',
        roleIds: [],
      },

      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入电子邮箱', trigger: 'blur' },
          { min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur' }
        ],
        roleIds: [
          { type: 'array', required: true, message: '请输入角色id', trigger: 'blur' }
        ]
      },
      options: [],
      value1: [],
    };
  },
  mounted() {
    this.localRoleList()
  },
  methods: {
    localRoleList(){
      let url = "http://localhost:9081/roles"
      console.log("开始请求role数据")
      //在请求头放Authorization内容 localStorage能够在当前网站存储信息
      this.axios
          .create({'headers':{'Authorization': localStorage.getItem('jwt')}})
          .get(url).then(response=>{
        let responseBody = response.data;
        if(responseBody.state != 20000){
          this.$message.error("请求role数据出错")
        }
        console.log("role参数：")
        console.log(responseBody.data)
        this.options = responseBody.data
      })
    },
    submitForm(formName) {
      /*validate：对整个表单进行校验的方法，参数为一个回调函数。
      该回调函数会在校验结束后被调用，并传入两个参数：是否校验成功和未通过校验的字段。
      若不传入回调函数，则会返回一个 promise*/
      //遍历每一个值
      this.$refs[formName].validate((valid) => {
        if (valid) {

          let url = 'http://localhost:9081/admins/add-new'
          console.log('url:' + url)
          //将json对象转为fromdata格式，不需要加@RequestBody
          let formData = this.qs.stringify(this.ruleForm);
          console.log('请求参数')
          //不要写字符串的拼接，因为无法展开参数内容
          console.log(formData)

          this.axios
              .create({'headers':{'Authorization': localStorage.getItem('jwt')}})
              .post(url,formData).then(response=>{
            let responseBody = response.data
            console.log(responseBody)
            if(responseBody.state == 20000){
              this.$message.success("添加成功")
              //进行调试的时候不要加重置表单，数据打桩输出会影响
              this.resetForm(formName)
            }else {
              this.$message.error(responseBody.message)
            }

          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>

</style>